import requests
from lxml import etree
import os
from time import sleep

start_url = 'http://www.bbsnet.com/biaoqingbao/page/%d'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
os.mkdir('./表情包')

for page_num in range(1,10):
    url = format(start_url % page_num)
    page_text = requests.get(url=url,headers=headers).text
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//*[@id="post_container"]/li')
    sleep(3)
    for li in li_list:
        detail_url = li.xpath('./div[1]/a/img/@src')[0]
        img_name = li.xpath('./div[1]/a/img/@alt')[0] + '.jpg'
        img = requests.get(url=detail_url,headers=headers).content
        img_path= './表情包/' + img_name
        with open(img_path,'wb') as f:
            print('保存成功')
            f.write(img)
            sleep(0.5)